$(function() {
	$("#commonPage").load("./common.html");
	$("#changePassword").load("./chagePassword.html");

	//修改密码
	$("#changePassword").on("click", "#changePasswordBtn", function() {
		var originalPassword = $("#originalPassword").val();
		var newPassword = $("#newPassword").val();
		var confirmPassword = $("#confirmPassword").val();
		if (originalPassword == '') {
			notifyMsg('原密码不能为空');
			return;
		}
		if (newPassword == '') {
			notifyMsg('新密码不能为空');
			return;
		}
		if (newPassword != confirmPassword) {
			notifyMsg('确认密码与新密码不一致');
			return;
		}
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/user/changePassword",
			dataType: "json",
			data: {
				"originalPassword": originalPassword,
				"newPassword": newPassword,
				"confirmPassword": confirmPassword
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					window.location.href = './login.html';
				} else {
					notifyMsg(data.msg);
				}
			}
		});
	});

	//退出登陆
	$("#commonPage").on("click", "#log_out", function() {
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/user/logout",
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					window.location.href = './login.html';
				}
			}
		});
	});

	//获取登陆session
	$.ajax({
		type: "GET",
		url: "http://127.0.0.1:8080/user/getSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				var node = $("#user_info");
				node.empty();
				var html = data.data.userName + '<span class="user-level">欢迎访问</span><span class="caret"></span>';
				node.html(html);
				if (data.data.userName != 'admin') {
					$("#userMrg").remove();
				}
			} else {
				window.location.href = './login.html';
			}
		}
	});
});

function notifyMsg(msg) {
	var notify = $.notify({
		// options
		icon: 'glyphicon glyphicon-warning-sign',
		title: '警告',
		message: msg,
		target: '_blank'
	}, {
		// settings
		element: 'body',
		position: null,
		type: "warning",
		allow_dismiss: true,
		newest_on_top: true,
		showProgressbar: false,
		placement: {
			from: "top",
			align: "center"
		},
		offset: 20,
		spacing: 10,
		z_index: 10001,
		delay: 5000,
		timer: 1000,
		url_target: '_blank'
	});

	return notify;
}

Date.prototype.Format = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
		"h+": this.getHours(), //小时
		"m+": this.getMinutes(), //分
		"s+": this.getSeconds(), //秒
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度
		"S": this.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" +
			o[k]).substr(("" + o[k]).length)));
	return fmt;
}
